package com.hhkj.admin.bank.common.component.creator;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.hhkj.admin.bank.common.cont.BankConst;
import com.hhkj.admin.bank.common.utils.BankCreateUtils;
import com.hhkj.admin.bank.domain.Bank;
import com.hhkj.admin.bank.domain.BankRule;
import com.hhkj.admin.bank.model.req.BankReq;
import com.hhkj.admin.question.common.cont.QuestionTypeEnum;
import com.hhkj.admin.question.common.utils.QuestionListUtils;
import com.hhkj.admin.question.domain.Question;
import com.hhkj.admin.question.model.req.QuestionReq;
import com.hhkj.admin.question.model.res.QuestionRes;
import com.ruoyi.common.exception.GlobalException;

import java.util.*;
import java.util.stream.Collectors;

/**
 * BankUtils
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/6/8 13:41
 */
public class BankCreator
{
    /** 新建题库名称 */
    public static String createBankName(String courseName, Integer amount)
    {
        return courseName + DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN) + ((amount < 1000 ? String.format("%03d", amount) : amount));
    }

    /** 新建题库名称 */
    public static String createBankName(String courseName)
    {
        return courseName + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
    }

    /**
     * 创建题库信息
     *
     * @param questionGroupList excel读取的数据信息
     * @param bankName          题库名称
     * @param courseId          科目id
     * @param courseParentId    类别id
     * @param courseBankTypeId  类别类型id
     * @param remark            备注信息
     * @return 题库信息
     */
    public static BankReq createBank(List<QuestionReq> questionGroupList, String bankName, Long courseId,
                                     Long courseParentId, Long courseBankTypeId, String remark)
    {
        // 获取案例题题目数量
        List<QuestionReq> caseQuestionList = questionGroupList.stream()
                .filter(finalQuestionDo -> QuestionTypeEnum.CASE.getCode().equals(finalQuestionDo.getTypeCode()))
                .collect(Collectors.toList());

        Integer caseGroupSize = 0;

        /*if (CollectionUtil.isNotEmpty(caseQuestionList)) {
            for (QuestionReq question : caseQuestionList) {
                if (caseGroupSize == 0) {
                    caseGroupSize = question.getQuestionList().size();
                    continue;
                }
                if (!caseGroupSize.equals(question.getQuestionList().size())) {
                    throw new GlobalException("案例题题目格式不统一");
                }
            }
        }*/
        // 题目组数统计
        Map<String, Integer> typeAmountConf = questionGroupList.stream().collect(Collectors.groupingBy(Question::getTypeCode
                , Collectors.collectingAndThen(Collectors.toList(), List::size)));
        return new BankReq(bankName, courseId, courseParentId,
                courseBankTypeId, typeAmountConf, caseGroupSize, remark, questionGroupList, QuestionListUtils.countGroupReqAmount(questionGroupList));
    }

    /**
     * 创建题库信息
     *
     * @param questionGroupList excel读取的数据信息
     * @param bankName          题库名称
     * @param remark            备注信息
     * @return 题库信息
     */
    public static BankReq createBank(List<QuestionReq> questionGroupList, String bankName, String remark)
    {
        // 获取案例题题目数量
        List<QuestionReq> caseQuestionList = questionGroupList.stream()
                .filter(finalQuestionDo -> QuestionTypeEnum.CASE.getCode().equals(finalQuestionDo.getTypeCode()))
                .collect(Collectors.toList());

        Integer caseGroupSize = 0;

        if (CollectionUtil.isNotEmpty(caseQuestionList)) {
            for (QuestionReq question : caseQuestionList) {
                if (caseGroupSize == 0) {
                    caseGroupSize = question.getQuestionList().size();
                    continue;
                }
                if (!caseGroupSize.equals(question.getQuestionList().size())) {
                    throw new GlobalException("案例题题目格式不统一");
                }
            }
        }
        // 题目组数统计
        Map<String, Integer> typeAmountConf = questionGroupList.stream().collect(Collectors.groupingBy(Question::getTypeCode
                , Collectors.collectingAndThen(Collectors.toList(), List::size)));
        return new BankReq(bankName, typeAmountConf, caseGroupSize, remark, questionGroupList, QuestionListUtils.countGroupReqAmount(questionGroupList));
    }


    /**
     * 创建练习题库信息
     *
     * @param totalBankPo 题库信息
     * @param rulePo      规则信息
     * @return 新题库信息
     */
    public static BankReq createPractiseBank(Bank totalBankPo, BankRule rulePo, List<QuestionRes> questionGroupList)
    {
        // 创建练习题库信息
        BankReq practiseBank = totalBankPo.createChild(BankConst.Type.PRACTISE, rulePo.getId());
        Map<String, List<QuestionRes>> totalQuestionListGroupByTypeCode
                = questionGroupList
                .stream()
                .collect(Collectors.groupingBy(QuestionRes::getTypeCode));

        // 练习题库列表
        List<QuestionReq> practiseQuestionList = new ArrayList<>();
        for (QuestionTypeEnum questionType : QuestionTypeEnum.values()) {
            // 没有对应题目 跳过
            if (!totalQuestionListGroupByTypeCode.containsKey(questionType.getCode())) {
                continue;
            }
            BankCreateUtils.extractQuestionList(
                    totalQuestionListGroupByTypeCode.get(questionType.getCode()),
                    practiseQuestionList,
                    rulePo.getPracticePercent());
        }
        // 题库题目信息保存
        practiseBank.setQuestionGroupList(practiseQuestionList);
        practiseBank.setQuestionAmount(QuestionListUtils.countGroupReqAmount(practiseQuestionList));
        return practiseBank;
    }

    /**
     * 创建练习题库信息
     *
     * @param totalBankPo       题库信息
     * @param ruleId            规则数据id
     * @param questionGroupList 题目组列表
     * @return 新题库信息
     */
    public static BankReq createPractiseBank(Bank totalBankPo, Long ruleId, List<QuestionReq> questionGroupList)
    {
        // 创建练习题库信息
        BankReq practiseBank = totalBankPo.createChild(BankConst.Type.PRACTISE, ruleId);
        // 题库题目信息保存
        practiseBank.setQuestionGroupList(questionGroupList);
        practiseBank.setQuestionAmount(QuestionListUtils.countGroupReqAmount(questionGroupList));
        return practiseBank;
    }

    /**
     * 创建测试题库信息
     * 20230615:
     *
     * @param bankPo 题目类型
     * @param rulePo 选题规则
     */
    @Deprecated
    public static BankReq createTestBank(Bank bankPo, BankRule rulePo, List<QuestionRes> totalQuestionGroupList, List<QuestionRes> practiseQuestionGroupList)
    {
        // 创建测试题库
        BankReq testBankReq = bankPo.createChild(BankConst.Type.TEST, rulePo.getId());
        // 获取各个部分题库题目列表信息
        Collection<QuestionRes> residueQuestionList = CollectionUtil.subtract(totalQuestionGroupList, practiseQuestionGroupList);

        // 题目分组预备
        Map<String, List<QuestionRes>> practiseQuestionListGroupByTypeCode
                = practiseQuestionGroupList.stream().collect(Collectors.groupingBy(QuestionRes::getTypeCode));
        Map<String, List<QuestionRes>> residueQuestionListGroupByTypeCode
                = residueQuestionList.stream().collect(Collectors.groupingBy(QuestionRes::getTypeCode));

        // 遍历题目列表
        List<QuestionReq> testQuestionList = new ArrayList<>();
        for (QuestionTypeEnum questionType : QuestionTypeEnum.values()) {
            if (!practiseQuestionListGroupByTypeCode.containsKey(questionType.getCode())
                    && !residueQuestionListGroupByTypeCode.containsKey(questionType.getCode())) {
                continue;
            }
            // 练习题库相关数量
            BankCreateUtils.extractQuestionList(practiseQuestionListGroupByTypeCode.get(questionType.getCode()), testQuestionList,
                    rulePo.getTestPracticePercent());

            // 剩余题目列表信息
            BankCreateUtils.extractQuestionList(residueQuestionListGroupByTypeCode.get(questionType.getCode()), testQuestionList,
                    rulePo.getTestResiduePercent());
        }
        testBankReq.setQuestionGroupList(testQuestionList);
        testBankReq.setQuestionAmount(QuestionListUtils.countGroupReqAmount(testQuestionList));
        return testBankReq;
    }

    /**
     * 创建测试题库信息
     *
     * @param bankPo            题库信息
     * @param questionGroupList 题目列表
     */
    public static BankReq createTestBank(Bank bankPo, Long ruleId, List<QuestionReq> questionGroupList)
    {
        // 创建测试题库
        BankReq testBankReq = bankPo.createChild(BankConst.Type.TEST, ruleId);
        testBankReq.setQuestionGroupList(questionGroupList);
        testBankReq.setQuestionAmount(QuestionListUtils.countGroupReqAmount(questionGroupList));
        return testBankReq;
    }

    /**
     * 创建测试题库信息
     *
     * @param bankPo 题目类型
     * @param rulePo 选题规则
     */
    public static BankReq createTestBank(Bank bankPo, BankRule rulePo, List<QuestionRes> totalQuestionGroupList)
    {
        // 创建测试题库
        BankReq testBankReq = bankPo.createChild(BankConst.Type.TEST, rulePo.getId());
        List<QuestionReq> testQuestionList = BeanUtil.copyToList(totalQuestionGroupList, QuestionReq.class);
        testBankReq.setQuestionGroupList(testQuestionList);
        testBankReq.setQuestionAmount(QuestionListUtils.countGroupReqAmount(testQuestionList));
        rulePo.matchBankErrorInfo(testBankReq);
        return testBankReq;
    }


}
